<?php
class manualController {
	public $structure;
	
	function __construct($path){
		$xmlFile = $path.DIRECTORY_SEPARATOR.'structure.xml';
		$this->structure = new DOMDocument();
		$this->structure->load( $xmlFile );
	 		
	}

	public function getChaptersArray(){
		$chaptersArray = array();
		$manual = $this->structure->documentElement;
		$level = 0;
		$chaptersArray = $this->addChaptersToArray($manual,$chaptersArray, $level);
		/*foreach ($manual->childNodes AS $chapGroup)
  		{
  			
  			if($chapGroup->nodeName=='chapters'){
  				foreach ($chapGroup->childNodes AS $chapter){
  					if($chapter->nodeName=='chapter'){
  						$chaptersArray[]=$chapter;
  					}
  				}	
  			}
  		}*/
		return $chaptersArray;
	}
	
	private function addChaptersToArray($node, $array, $level){
		foreach ($node->childNodes AS $chapGroup)
  		{	
  			if($chapGroup->nodeName=='chapters'){
				foreach ($chapGroup->childNodes AS $chapter){
		  			if($chapter->nodeName=='chapter'){
		  				$chapterConfig = new chapterConfig();
		  				$chapterConfig->level = $level+1;
		  				$chapterConfig->title = $chapter->getAttribute('title');
		  				$chapterConfig->filename = $chapter->getAttribute('filename');
		  				$array[]=$chapterConfig;
		  				$array = $this->addChaptersToArray($chapter, $array, $level+1);
		  			}
				}
  			}
  		}
  		return $array;	
	}
	
	public static function compose($chapterName){
		$templatePath = sfConfig::get('app_manual_dir','/manual');
		if (!file_exists($templatePath.DIRECTORY_SEPARATOR.$chapterName.'.html')){
			return 'Error - The following chapter is missing:'.' '.$templatePath.DIRECTORY_SEPARATOR.$chapterName.'.html';
		}
				
		$templateHtml = file_get_contents($templatePath.DIRECTORY_SEPARATOR.$chapterName.'.html');
			
		//$templateHtml = str_replace('{title}',$newsLetter->descripcion, $templateHtml);
		//$templateHtml = str_replace('{descripcion}',$newsLetter->descripcion, $templateHtml);
		//$templateHtml = str_replace('{fecha}',$newsLetter->fecha, $templateHtml);
		
		$manual_dir_name = sfConfig::get('app_manual_dir_name','/manual');
		$manual_dir_name = str_replace('\\','/', $manual_dir_name);
		$templateHtml = str_replace('src="img/','src="/'.$manual_dir_name.'/img/', $templateHtml);
		$templateHtml = str_replace('src="swf/','src="/'.$manual_dir_name.'/swf/', $templateHtml);
		$templateHtml = str_replace('value="swf/','value="/'.$manual_dir_name.'/swf/', $templateHtml);
		$startPos = strpos($templateHtml,'#swf:',0);
		while ($startPos > 0) {
			$string = strstr($templateHtml,'#swf:');
			$finalPos = strpos($templateHtml,'#',$startPos + 4)+1;
			$stringOri = substr($templateHtml,$startPos,$finalPos - $startPos);
			//params
			$parStartPos = strpos($stringOri,'{params:',0);
			if($parStartPos>0){
				$parFinalPos = strpos($stringOri,'}',$parStartPos + 8);
				$parOri = substr($stringOri, $parStartPos ,$parFinalPos +1 );
				$start = $parStartPos + 8;
				$params = substr($stringOri, $start ,$parFinalPos - $start);
				$templateHtml = str_replace($parOri,'', $templateHtml); //I take out the params string from template
				$stringOri = substr($stringOri,0,$parStartPos);
				
			}else{
				$params = '';
			}
			//fin params
			$string = str_replace('#','', $stringOri);
			$string = str_replace('swf:','', $string);
			$iframe = '<iframe src="/'.$manual_dir_name.'/swf/viewer.php?src='.$string.'" frameborder="0" '.$params.' width="100%" scrolling="no"><p>Your browser does not support iframes.</p></iframe>';
			$templateHtml = str_replace($stringOri,$iframe, $templateHtml);
			$startPos = strpos($templateHtml,'#swf:',$finalPos + 1);
		}
		return 	$templateHtml;	
	}
}

class chapterConfig {
	public $level;
	public $title;
	public $filename;
}